#include "cmsis_os2.h"
#include "lvgl.h"


extern osMutexId_t LvglMutexHandle;

lv_obj_t* slider_label = NULL;

static void slider_event_cb(lv_event_t *e)
{
    lv_obj_t* target = lv_event_get_target(e);                                          /* 获取触发源 */
    lv_event_code_t code = lv_event_get_code(e);                                        /* 获取事件类型 */
    if(code == LV_EVENT_VALUE_CHANGED)
    {
        lv_label_set_text_fmt(slider_label, "%d%%", lv_slider_get_value(target));       /* 获取当前值，更新进度条百分比 */
    }
}

void StartLvglTask(void *argument)
{
    osMutexAcquire(LvglMutexHandle, osWaitForever);

    int32_t  with = lv_obj_get_width(lv_scr_act());

    lv_obj_t* slider = lv_slider_create(lv_scr_act());                                  /* 创建滑块 */
    lv_obj_set_size(slider, with / 2, 20);                                              /* 设置大小 */
    lv_obj_center(slider);                                                              /* 设置位置 */
    lv_slider_set_value(slider, 50, LV_ANIM_OFF);                                       /* 设置当前值，关闭动画 */
    lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);         /* 添加事件 */

    lv_obj_t* sound_label = lv_label_create(lv_scr_act());                              /* 创建音量标签 */
    lv_label_set_text(sound_label, LV_SYMBOL_VOLUME_MAX);                               /* 设置文本内容：音量图标 */
    lv_obj_set_style_text_font(sound_label, &lv_font_montserrat_24, LV_STATE_DEFAULT);  /* 设置字体 */
    lv_obj_align_to(sound_label, slider, LV_ALIGN_OUT_LEFT_MID, -20, 0);                /* 设置位置 */

    slider_label = lv_label_create(lv_scr_act());                                       /* 创建百分比标签 */
    lv_label_set_text_fmt(slider_label, "%d%%", lv_slider_get_value(slider));           /* 设置文本内容 */
    lv_obj_set_style_text_font(slider_label, &lv_font_montserrat_24, LV_STATE_DEFAULT); /* 设置字体 */
    lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_RIGHT_MID, 20, 0);               /* 设置位置 */


    osMutexRelease(LvglMutexHandle);

    for(;;)
    {
        osMutexAcquire(LvglMutexHandle, osWaitForever);
        lv_timer_handler();
        osMutexRelease(LvglMutexHandle);
        osDelay(5);
    }
}
